package com.ben.multiple.shard.proxy;

import com.ben.multiple.shard.proxy.mp.Order;
import com.ben.multiple.shard.proxy.mp.OrderMapper;
import com.ben.multiple.shard.proxy.mp.OrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
@RestController
@RequestMapping("/proxy")
public class ProxyController {
    static AtomicInteger atomicInteger = new AtomicInteger(LocalDateTime.now().getSecond());
    static AtomicLong atomicLong = new AtomicLong(LocalDateTime.now().getMinute());
    static String[] cs = {"A", "B", "C"};

    @Resource
    private OrderService orderService;


    @GetMapping("/insertOrder")
    public Map<String, Object> insertOrder() {
        List<Order> list = new ArrayList<>();
        list.add(new Order(null, new Random().nextInt(2), atomicInteger.getAndIncrement(), atomicLong.getAndIncrement(), cs[new Random().nextInt(3)]));
        list.add(new Order(null, new Random().nextInt(2), atomicInteger.getAndIncrement(), atomicLong.getAndIncrement(), cs[new Random().nextInt(3)]));
        list.add(new Order(null, new Random().nextInt(2), atomicInteger.getAndIncrement(), atomicLong.getAndIncrement(), cs[new Random().nextInt(3)]));
        list.add(new Order(null, new Random().nextInt(2), atomicInteger.getAndIncrement(), atomicLong.getAndIncrement(), cs[new Random().nextInt(3)]));
        list.add(new Order(null, new Random().nextInt(2), atomicInteger.getAndIncrement(), atomicLong.getAndIncrement(), cs[new Random().nextInt(3)]));

        orderService.saveBatch(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("all", list);
        return map;
    }

}
